<html>
<style>
  body {margin : 10px 10px; font-family: 'Segoe UI'}
  table {border-collapse: collapse}
  td, th {border: solid silver 1px; font-weight: normal; font-size: 10px; padding: 2px 2px}
  td:hover {background: yellow}
  #weekmap td {height: 16px; width: 18px}
  th, tr td::first-child {background: lightyellow}
  #weekmap td:nth-child(odd) {border-left: solid #ddd 1px}
  .color {display: inline-block; margin: 3px 3px; padding: 2px 5px; border-bottom: solid transparent 3px; cursor: pointer}
  .colorsel {border-bottom: solid orange 3px; font-weight: bold; }
  h3 {color: gray; font-weight: normal}
  .gray {background: silver}
  {{#colors}}
   .{{id}} {background: {{color}}; }
  {{/colors}}
</style>

<report-objects>
 <object>
  <script>
   s := '<colors><color id="color1" name="Green" color="#2dbd78"/><color id="color2" name="Red" color="#f96908"/><color id="color3" name="Blue" color="#128ac6"/></colors>';
   Report.RegisterVariable('colors', ReleaseObject(THtXMLNode.Create(s)), true);
  </script>
 </object>
</report-objects>

<weekmap></weekmap>

<yearmap></yearmap>

<script type="passcript">

var selectedcolor: string = '';

procedure setselected();
begin
  if this.Col > 0 then
  begin
    FirstCell := this;
    Map := IfThen(this.Table.id='yearmap', YearMap, WeekMap);
    UpdateSelection(FirstCell, FirstCell);
    SelectionMode := true;
  end;
end;

procedure UpdateSelection(C1, C2: TElement);
begin
  for C in $('#'+C1.Table.Id+' td') do
  begin
    if C.HasClass('gray') then
      continue;
    C.ClassName := '';
    if InRange(C.Col, C1.Col, C2.Col) and InRange(C.Row, C1.Row, C2.Row) then
      C.addClass(SelectedColor)
    else begin
      for E in Map do
        if InRange(C.Col, E.Attr['starthour'], E.Attr['endhour']) and
          InRange(C.Row, E.Attr['startday'], E.Attr['endday']) then
      C.addClass(E.Attr['id'])
    end;
  end;
  LastCell := C2;
  document.Repaint;
end;

procedure addselected();
begin
  if SelectionMode and (this.tag = 'td') then
    UpdateSelection(FirstCell, this);
end;

procedure AddItem;
begin
  Item := Map.Add('item');
  Item.Attr['id'] := SelectedColor;
  Item.Attr['startday'] := inttostr(FirstCell.Row);
  Item.Attr['endday'] := inttostr(LastCell.Row);
  Item.Attr['starthour'] := inttostr(FirstCell.Col);
  Item.Attr['endhour'] := inttostr(LastCell.Col);
end;

procedure onup();
begin
  SelectionMode := false;
  if Assigned(FirstCell) and Assigned(LastCell) then
    AddItem();
  FirstCell := nil;
end;

procedure changecolor;
begin
  SelectedColor := this.Attr['id'];
  $('.color').removeClass('colorsel');
  this.addClass('colorsel');
  document.Repaint;
end;

SelectionMode := false;
$('td').on('mousedown', 'setselected()');
$('td').on('mouseover', 'addselected()');
document.addEventListener('mouseup', @onup);

WeekMap := THtXMLNode.Create($('weekmap').html);
YearMap := THtXMLNode.Create($('yearmap').html);

</script>

 {{#colors}}
  <span id="{{id}}" class="color" onclick="changecolor()"><span class="{{id}}">&nbsp;&nbsp;&nbsp;</span>  {{name}}</span>
 {{/colors}}


<h3>Week map</h3>
<DATAPACKET id="weekmap">
 <METADATA>
  <FIELDS>
    <FIELD name="DAY" caption="Day" />
  </FIELDS>
  <SCRIPT>
  { creating fields }
  for i := 0 to 47 do
  begin
    F := Fields.Add('FIELD');
    F.Attr['name'] := 'H' + inttostr(i);
    F.Attr['caption'] := inttostr(i div 2)+'|';
    F.Attr['width'] := '24';
  end;
  { creating rows }
  for i:= 1 to 7 do
  begin
    R := RowData.Add('R');
     R.Attr['DAY'] := FormatDateTime('ddd', i+1); //DayNameEn(i);
  end;
  </SCRIPT>
 </METADATA>
 <ROWDATA/>
</DATAPACKET>

<h3>Year map</h3>
<DATAPACKET id="yearmap">
 <METADATA>
  <FIELDS>
    <FIELD name="MONTH" caption="Month" />
  </FIELDS>
  <SCRIPT>
  { creating fields }
  for i := 1 to 31 do
  begin
    F := Fields.Add('FIELD');
    F.Attr['name'] := 'M' + inttostr(i);
    F.Attr['caption'] := inttostr(i);
    F.Attr['width'] := '24';
  end;
  { creating rows }
  for i := 1 to 12 do
  begin
    R := RowData.Add('R');
    R.Attr['MONTH'] := FormatDateTime('MMMM', i*30); //MonthNameEn(i);
    for k := DaysInAMonth(1904, i) + 1 to 31 do
      R.Attr['M'+inttostr(k)+'_CLASS'] := 'gray';
  end;
  </SCRIPT>
 </METADATA>
 <ROWDATA/>
</DATAPACKET>


</html>
